#include "cantp_mainfunction.h"
#include "canif.h"

Cantp_HandlerStruct Cantp_Handler;

/* 初始化CANTP */
void CanTP_Init(void)
{
    Cantp_CanidRegister(&Cantp_Handler, CHANNEL, RESP_ID, REQU_ID);
    Cantp_CanRegister(&Cantp_Handler, Canif_Cantp_send_frame, Canif_Cantp_receive_frame);
    Cantp_AbilityRegister(&Cantp_Handler, STMIN, BLOCK_SIZE);
    Cantp_CallbackRegister(&Cantp_Handler, CHANNEL, Cantp_Tx_Callback, Cantp_Rx_Callback);
}

/* CANTP主函数 */
void Cantp_MainFunction(void)
{
    /* CANTP 发送处理 */
    Cantp_TxTask(&Cantp_Handler);

    /* CANTP 接收处理 */
    Cantp_RxTask(&Cantp_Handler, CALL_TASK, 0, RT_NULL, 0);
}

/* CANTP Send Call Back */
void Cantp_Tx_Callback(rt_uint32_t id, rt_bool_t results)
{
    rt_kprintf("Tx ID: %X \t Result: %s\r\n", id, (results) ? "TRUE" : "FALSE");
}

/* CANTP Receive Call Back */
void Cantp_Rx_Callback(rt_uint32_t id, rt_uint8_t *msg, rt_uint32_t size)
{
    #if 0
    rt_kprintf("Rx ID: %X\t", id);
    rt_kprintf("Length: %X\t", size);
    rt_kprintf("MSG: ");
    for (rt_uint32_t i = 0; i < size; i++)
    {
        rt_kprintf("%02X ", msg[i]);
    }
    rt_kprintf("\r\n");
    #endif

    rt_uint8_t response_msg[64] = {0};

    if (REQU_ID == id)
    {
        if (0x22 == msg[0]) /* 22服务 */
        {
            if (3 == size)  /* 长度判断 */
            {
                if (0xF1 == msg[1] && 0x93 == msg[2])   /* F193 读取硬件版本号 */
                {
                    response_msg[0] = 0x62; /* 肯定响应 */
                    response_msg[1] = 0xF1; /* DID */
                    response_msg[2] = 0x93;
                    rt_memcpy(response_msg + 3, "HW_STM32F407ZGT6_1.0", 20);
                    Cantp_Tx(&Cantp_Handler, RESP_ID, response_msg, 23);
                }
                else if (0xF1 == msg[1] && 0x95 == msg[2])  /* F195 读取软件版本号 */
                {
                    response_msg[0] = 0x62; /* 肯定响应 */
                    response_msg[1] = 0xF1; /* DID */
                    response_msg[2] = 0x95;
                    rt_memcpy(response_msg + 3, "SW_DEMO_Version_1.0", 19);
                    Cantp_Tx(&Cantp_Handler, RESP_ID, response_msg, 22);
                }
                else    /* 否定响应，请求超过限值 */
                {
                    response_msg[0] = 0x7F; /* 否定响应 */
                    response_msg[1] = 0x22; /* DID */
                    response_msg[2] = 0x31; /* NRC */
                    Cantp_Tx(&Cantp_Handler, RESP_ID, response_msg, 3);
                }
            }
            else    /* 否定响应，响应过长 */
            {
                response_msg[0] = 0x7F; /* 否定响应 */
                response_msg[1] = 0x22; /* DID */
                response_msg[2] = 0x14; /* NRC */
                Cantp_Tx(&Cantp_Handler, RESP_ID, response_msg, 3);
            }
        }
        else    /* 否定响应，不支持的服务 */
        {
            response_msg[0] = 0x7F; /* 否定响应 */
            response_msg[1] = msg[0];   /* DID */
            response_msg[2] = 0x11; /* NRC */
            Cantp_Tx(&Cantp_Handler, RESP_ID, response_msg, 3);
        }
    }
}